//
//  selectDateVC.m
//  Recycling
//
//  Created by jzd on 2018/5/23.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "selectDateVC.h"

@interface selectDateVC ()
@property (strong,nonatomic)UIDatePicker *datePicker;
@end

@implementation selectDateVC
-(UIDatePicker *)datePicker{
    if (_datePicker == nil) {
        _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, SJScreenW, 220)];
        // 设置日期选择控件的地区
        [_datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
        //默认为当天。
        [_datePicker setCalendar:[NSCalendar currentCalendar]];
        _datePicker.backgroundColor = [UIColor whiteColor];
        //    设置DatePicker的时区。
        //    默认为设置为：[datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
        
        //    设置DatePicker的日期。
        //    默认设置为:
        [_datePicker setDate:[NSDate date]];
        
        //    minimumDate设置DatePicker的允许的最小日期。
        //    maximumDate设置DatePicker的允许的最大日期
        
//        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//        NSDate *currentDate = [NSDate date];
//
//        NSDateComponents *comps = [[NSDateComponents alloc] init];
//
//        [comps setDay:10];//设置最大时间为：当前时间推后10天
//
//        NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
//
//        [comps setDay:0];//设置最小时间为：当前时间
//
//        NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
//
        if (self.minDate) {
            [_datePicker setMinimumDate:self.minDate];
        }
//
//        [_datePicker setMinimumDate:minDate];
        

        //    设置DatePicker的倒计时间.
        
        //    1) 设置日期选择的模
        if (self.datePickerMode) {
            [_datePicker setDatePickerMode:self.datePickerMode];
        }else{
            [_datePicker setDatePickerMode:UIDatePickerModeDate];
        }
        
        
        //    2) 设置倒计时的时长
        
        //    注意：设置倒计时时长需要在确定模式之后指定
        
        // 倒计时的时长，以秒为单位
        
        //    [myDatePicker setCountDownDuration:10 * 60];

       
    }
    return _datePicker;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BGCOLOR;
    
    
    [self addToolBar];
    [self.view addSubview:self.datePicker];
    //监听datepicker值的改变
//    [self.datePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
}
-(void)addToolBar{
    UIView *tool = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SJScreenW, 40)];
    tool.backgroundColor = BGCOLOR;
    UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:TITLECOLOR forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancelOnClick) forControlEvents:UIControlEventTouchUpInside];
    [tool addSubview:cancel];
    
    UIButton *sure = [[UIButton alloc]initWithFrame:CGRectMake(tool.width-80,0, 80, 40)];
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:TITLECOLOR forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sureOnClick) forControlEvents:UIControlEventTouchUpInside];
    [tool addSubview:sure];
    [self.view addSubview:tool];
}
-(void)cancelOnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)sureOnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if (self.datePickerMode == UIDatePickerModeDateAndTime) {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    }else{
        formatter.dateFormat = @"yyyy-MM-dd";
    }
    
    NSString *seleDate = [formatter stringFromDate:self.datePicker.date];
    if (self.dateBlock) {
        self.dateBlock(seleDate);
    }
    SJLog(@"%@",seleDate);
}
-(void)dateChange:(UIDatePicker *)datepicker{
    
    
}
-(void)dealloc{
    SJLog(@"selectDateVC挂了");
}


@end
